package com.dynamic.redis.config;

import org.springframework.beans.BeansException;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * TODO
 * @author 库琦
 * @version 1.0
 * @date 2022/6/14 14:35
 */
public  abstract  class RedisDataSourceContext  implements ApplicationContextAware {
    public static final String FIELD_DATASOURCE_NAME = "dataSourceName";

    protected ApplicationContext applicationContext;

    protected String dataSourceName;

    /**
     * 通过数据源名称获取该数据源对应的Redis配置
     */
    protected RedisProperties getRedisProperties() {
        RedisDataSourceProperties dataSourceProperties = applicationContext.getBean(RedisDataSourceProperties.class);
        // 通过数据源名称获取application配置文件中配置的该数据源的redis-properties
        return dataSourceProperties.getDatasource().get(dataSourceName);
    }





    protected ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public String getDataSourceName() {
        return dataSourceName;
    }

    public void setDataSourceName(String dataSourceName) {
        this.dataSourceName = dataSourceName;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

}
